package main

import (
	"fmt"
	"path/filepath"

	"demserver/src/elevation"
)

func main() {
	// Use the actual GeoTIFF file
	filename := "./demdata/scdjy.tif"

	// Get absolute path to the file
	absPath, err := filepath.Abs(filename)
	if err != nil {
		fmt.Printf("Failed to get absolute path: %v\n", err)
		return
	}

	fmt.Printf("Loading GeoTIFF file: %s\n", absPath)
	geoTiff, err := elevation.LoadGeoTIFF(absPath)
	if err != nil {
		fmt.Printf("Failed to load GeoTIFF: %v\n", err)
		return
	}
	fmt.Println("GeoTIFF loaded successfully")

	// Test with the provided high precision coordinates
	lat := 31.03021050140226
	lon := 103.55147461467699
	expected := 837.6951116196124

	fmt.Printf("TFW file parameters:\n")
	fmt.Printf("  A (pixel size in x-direction): %.14f\n", geoTiff.GeoTransform[1])
	fmt.Printf("  B (rotation about x-axis): %.14f\n", geoTiff.GeoTransform[2])
	fmt.Printf("  D (rotation about y-axis): %.14f\n", geoTiff.GeoTransform[4])
	fmt.Printf("  E (pixel size in y-direction): %.14f\n", geoTiff.GeoTransform[5])
	fmt.Printf("  C (x-coordinate of top-left pixel): %.14f\n", geoTiff.GeoTransform[0])
	fmt.Printf("  F (y-coordinate of top-left pixel): %.14f\n", geoTiff.GeoTransform[3])

	x, y, err := geoTiff.LatLonToPixel(lat, lon)
	if err != nil {
		fmt.Printf("Failed to convert coordinates (%.14f, %.14f): %v\n", lat, lon, err)
		return
	}

	fmt.Printf("\nInput coordinates:\n")
	fmt.Printf("  Lat: %.14f\n", lat)
	fmt.Printf("  Lon: %.14f\n", lon)
	fmt.Printf("Pixel coordinates:\n")
	fmt.Printf("  X: %.14f\n", x)
	fmt.Printf("  Y: %.14f\n", y)

	// Reverse calculation to verify
	reverseLon := geoTiff.GeoTransform[0] + x*geoTiff.GeoTransform[1] + y*geoTiff.GeoTransform[2]
	reverseLat := geoTiff.GeoTransform[3] + x*geoTiff.GeoTransform[4] + y*geoTiff.GeoTransform[5]

	fmt.Printf("\nReverse calculation:\n")
	fmt.Printf("  Calculated Lon: %.14f\n", reverseLon)
	fmt.Printf("  Original Lon: %.14f\n", lon)
	fmt.Printf("  Lon difference: %.14f\n", reverseLon-lon)
	fmt.Printf("  Calculated Lat: %.14f\n", reverseLat)
	fmt.Printf("  Original Lat: %.14f\n", lat)
	fmt.Printf("  Lat difference: %.14f\n", reverseLat-lat)

	// Get elevation value
	elevationVal, err := geoTiff.GetElevation(x, y)
	if err != nil {
		fmt.Printf("Failed to get elevation at (%.14f, %.14f): %v\n", x, y, err)
		return
	}

	fmt.Printf("\nElevation results:\n")
	fmt.Printf("  Expected elevation: %.14f\n", expected)
	fmt.Printf("  Actual elevation: %.14f\n", elevationVal)
	fmt.Printf("  Difference: %.14f\n", elevationVal-expected)
}
